// 新增
function addFriendChainFun() {

    $("#addFriendChainBtn").on("click", function () {
        var friendChainState = $(".addFriendChainState:checked").val();
        var friendChainName = $(".addFriendChainName").val();
        var friendChainUrl = $(".addFriendChainUrl").val();
        var friendChainIcon = $(".addFriendChainIcon").val();
        var friendChainIntroduce = $(".addFriendChainIntroduce").val();
        if (friendChainName != null && friendChainName.length != 0
            && friendChainState != null && friendChainState.length != 0
            && friendChainUrl != null && friendChainUrl.length != 0
            && friendChainIcon != null && friendChainIcon.length != 0
            && friendChainIntroduce != null && friendChainIntroduce.length != 0) {
            $.ajax({
                type: "GET",
                url: "/friendChainManage/addFriendChain",
                dataType: "json",
                data: {
                    friendChainName: friendChainName,
                    friendChainState: friendChainState,
                    friendChainUrl: friendChainUrl,
                    friendChainIcon: friendChainIcon,
                    friendChainIntroduce: friendChainIntroduce
                },
                success: function (data) {
                    if (data.result == "success") {
                        alert("添加成功");
                        window.location.reload();
                    } else {
                        alert("添加失败");
                    }
                },
                error: function () {
                    alert("服务器错误，操作失败");
                }
            });
        } else {
            alert("请输入或设置所有信息，不能留有空白");
        }
    });
}

// 查看，可以进行更新
function friendChainFun(friendChainId) {
    $.ajax({
        type: "GET",
        url: "/friendChainManage/getFriendChain?friendChainId=" + friendChainId,
        dataType: "json",
        success: function (data) {
            var friendChain = data.friendChain;
            if (friendChain != null) {
                $(".friendChainId").val(friendChain.friendChainId);
                $(".friendChainName").val(friendChain.friendChainName);
                $(".friendChainUrl").val(friendChain.friendChainUrl);
                $(".friendChainIcon").val(friendChain.friendChainIcon);
                $(".friendChainIntroduce").val(friendChain.friendChainIntroduce);
                if (friendChain.friendChainState == 0) {
                    $(".friendChainState").eq(0).attr("checked", "true");
                } else {
                    $(".friendChainState").eq(1).attr("checked", "true");
                }
            }
        },
        error: function () {
            alert("服务器错误，操作失败");
        }
    });
    // 更新
    $("#updateFriendChainBtn").on("click", function () {
        var friendChainId = $(".friendChainId").val();
        var friendChainName = $(".friendChainName").val();
        var friendChainState = $(".friendChainState:checked").val();
        var friendChainUrl = $(".friendChainUrl").val();
        var friendChainIcon = $(".friendChainIcon").val();
        var friendChainIntroduce = $(".friendChainIntroduce").val();
        if (friendChainName != null && friendChainName.length != 0
            && friendChainState != null && friendChainState.length != 0
            && friendChainUrl != null && friendChainUrl.length != 0
            && friendChainIcon != null && friendChainIcon.length != 0
            && friendChainIntroduce != null && friendChainIntroduce.length != 0) {
            $.ajax({
                type: "POST",
                url: "/friendChainManage/updateFriendChain",
                dataType: "json",
                data: {
                    friendChainId: friendChainId,
                    friendChainName: friendChainName,
                    friendChainState: friendChainState,
                    friendChainUrl: friendChainUrl,
                    friendChainIcon: friendChainIcon,
                    friendChainIntroduce: friendChainIntroduce
                },
                success: function (data) {
                    if (data.result == "success") {
                        alert("更新成功");
                        window.location.reload();
                    } else {
                        alert("更新失败");
                    }
                },
                error: function () {
                    alert("服务器错误，操作失败");
                }
            });
        } else {
            alert("请输入或设置所有信息，不能留有空白");
        }
    });
}

// 删除友链
function deleteFriendChainFun(friendChainId) {
    $("#deleteFriendChainBtn").on("click", function () {
        $.ajax({
            type: "GET",
            url: "/friendChainManage/deleteFriendChain?friendChainId=" + friendChainId,
            dataType: "json",
            success: function (data) {
                if (data.result == "success") {
                    alert("删除成功");
                    window.location.reload();
                } else {
                    alert("删除失败");
                }
            },
            error: function () {
                alert("服务器错误，操作失败");
            }
        });
    });
}